package com.lagou.sqlSession;


import com.lagou.config.BoundSql;
import com.lagou.pojo.Configuration;
import com.lagou.pojo.MappedStatement;
import com.lagou.utils.GenericTokenParser;
import com.lagou.utils.ParameterMapping;
import com.lagou.utils.ParameterMappingTokenHandler;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;

public class SimpleExecutor implements  Executor {


    @Override                                                                                //user
    public <E> List<E> query(Configuration configuration, MappedStatement mappedStatement, Object... params) throws Exception {

        PreparedStatement statement = null;
        try{
            BoundSql boundSql = getBoundSql(mappedStatement.getSql());
            StatementHandler statementHandler = new SimpleStatementHandler(configuration,mappedStatement,params);
            statement = prepareStatement(statementHandler,configuration,boundSql);
            return statementHandler.query(statement);
        }finally {
            closeStatement(statement);
        }


    }
    private PreparedStatement prepareStatement(StatementHandler statementHandler, Configuration configuration, BoundSql boundSql) throws Exception {
        Connection connection = configuration.getDataSource().getConnection();
        PreparedStatement preparedStatement = connection.prepareStatement(boundSql.getSqlText());
        statementHandler.prepareParams(preparedStatement,boundSql);
        return preparedStatement;
    }

    @Override
    public int update(Configuration configuration, MappedStatement mappedStatement, Object[] params) throws Exception {
        PreparedStatement statement = null;
        try{
            BoundSql boundSql = getBoundSql(mappedStatement.getSql());
            StatementHandler statementHandler = new SimpleStatementHandler(configuration,mappedStatement,params);
            statement = prepareStatement(statementHandler,configuration,boundSql);
            return statementHandler.update(statement);
        }finally {
            closeStatement(statement);
        }

    }

    private Class<?> getClassType(String paramterType) throws ClassNotFoundException {
        if(paramterType!=null){
            Class<?> aClass = Class.forName(paramterType);
            return aClass;
        }
         return null;

    }

    private void closeStatement(Statement statement) {
        if (statement != null) {
            try {
                statement.close();
            } catch (SQLException e) {
                // ignore
            }
        }
    }



    /**
     * 完成对#{}的解析工作：1.将#{}使用？进行代替，2.解析出#{}里面的值进行存储
     * @param sql
     * @return
     */
    private BoundSql getBoundSql(String sql) {
        //标记处理类：配置标记解析器来完成对占位符的解析处理工作
        ParameterMappingTokenHandler parameterMappingTokenHandler = new ParameterMappingTokenHandler();
        GenericTokenParser genericTokenParser = new GenericTokenParser("#{", "}", parameterMappingTokenHandler);
        //解析出来的sql
        String parseSql = genericTokenParser.parse(sql);
        //#{}里面解析出来的参数名称
        List<ParameterMapping> parameterMappings = parameterMappingTokenHandler.getParameterMappings();

        BoundSql boundSql = new BoundSql(parseSql,parameterMappings);
         return boundSql;

    }


}
